//
//  ZBYUserModel.swift
//  ZBYBlog
//
//  Created by zby on 16/2/27.
//  Copyright © 2016年 zby. All rights reserved.
//

import UIKit

class ZBYUserModel: NSObject , NSCoding{
    
//    返回值字段	字段类型	字段说明
    var access_token : String?
//    access_token	string	用于调用access_token，接口获取授权后的access token。
    var expires_in : NSTimeInterval = 0
//    expires_in	string	access_token的生命周期，单位是秒数。
//    remind_in	string	access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
    var uid : String?
//    uid	string	当前授权用户的UID。
    
    //用户昵称
    var screen_name: String?
    //用户头像
    var profile_image_url: String?
    
    //KVC赋值
    init(dic: [String: NSObject]) {
        super.init()
        setValuesForKeysWithDictionary(dic)
    }
    //防止崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    func save(file: String){
        
        //拼接沙盒路径
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
        let filePath = (path! as NSString).stringByAppendingPathComponent(file)
        
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
        
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder){
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_in = aDecoder.decodeObjectForKey("expires_in") as!NSTimeInterval
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
    }
}
